/**
 * 
 */
package com.tw.trips.processor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tw.trips.domain.Alert;
import com.tw.trips.domain.Itinerary;
import com.tw.trips.domain.Location;
import com.tw.trips.domain.Traveller;
import com.tw.trips.domain.Trip;
import com.tw.trips.service.LocationService;
import com.tw.trips.service.TravellerService;
import com.tw.trips.service.TripAlertService;
import com.tw.trips.util.IrctcStationCodes;

/**
 * @author M1005264
 * 
 */
@Service
public class IrctcItineraryProcessor {

	@Autowired
	private TravellerService travelerService;

	@Autowired
	private LocationService locationService;
	
	@Autowired
	private IrctcStationCodes stationCodes;

	@Autowired
	private TripAlertService tripAlertService;
	
	public void process(String senderEmail, String tripData) {
		Traveller travlr = checkAndGeTraveller(senderEmail);
		addTrip(travlr, tripData);
		
		System.out.println("Traveller Service" + travelerService.getTraveller(senderEmail));
	}

	private Traveller checkAndGeTraveller(String senderEmail) {
		Traveller retTravlr = travelerService.getTraveller(senderEmail);
		if (retTravlr == null) {
			retTravlr = travelerService.register(senderEmail);
		}
		return retTravlr;
	}

	public void addTrip(Traveller travlr, String tripData) {
		Trip newTrip = getTripData(travlr, tripData);
		
		System.out.println(newTrip);
		travelerService.addTrip(travlr, newTrip);
	}

	private Trip getTripData(Traveller travlr, String message) {
		String[] lines = message.split("\\n");
		String trainNO = null;
		String trainName = null;
		String from = null;
		String to = null;
		Date travelDate = null;
		for (int i = 0; i < lines.length; i++) {
			if (lines[i].trim().startsWith("Booking details:")) {
				System.out.println(lines[i + 3] + "- " + lines[i + 5]);
				System.out.println(lines[i + 7] + "- " + lines[i + 9]);
				trainNO = lines[i + 9];
				System.out.println(lines[i + 11] + "- " + lines[i + 13]);
				trainName = lines[i + 13];
				System.out.println(lines[i + 15] + "- " + lines[i + 17]);
				System.out.println(lines[i + 19] + "- " + lines[i + 21]);
				System.out.println(lines[i + 23] + "- " + lines[i + 25]);
				try {
					travelDate = new SimpleDateFormat("yyyy-MM-dd")
							.parse(lines[i + 25]);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(lines[i + 27] + "- " + lines[i + 29]);
				System.out.println(lines[i + 31] + "- " + lines[i + 33]);
				from = lines[i + 33];
				System.out.println(lines[i + 35] + "- " + lines[i + 37]);
				to = lines[i + 37];
				System.out.println(lines[i + 39] + "- " + lines[i + 41]);

				break;
			}
		}

		Itinerary itnry = new Itinerary();
		itnry.setCarrierNum(trainNO);
		itnry.setCarrierName(trainName);
		itnry.setFromCode(from);
		itnry.setToCode(to);
		itnry.setTravelDate(travelDate);
		Alert alert = tripAlertService.getAlertForItineary(itnry);
		alert.setToBeAlerted(travlr);
		travelerService.addAlert(alert);
		
		Location fromLocation = locationService.getOrAddLocation(stationCodes
				.getLocationFromCode(from));
		Location toLocation = locationService.getOrAddLocation(stationCodes
				.getLocationFromCode(to));

		Trip trip = new Trip();
		trip.setCreatedBy(travlr);
		trip.setCreatedOn(new Date());
		trip.setSource(fromLocation);
		trip.setDestination(toLocation);
		trip.setTravelEndDate(travelDate);
		trip.setTravelStartDate(travelDate);
		trip.setItinerary(itnry);

		// TODO Auto-generated method stub
		return trip;
	}
}
